/**
 * Created by Administrator on 2017/3/28.
 */

import {Injectable} from '@angular/core';
import {HttpService} from "../../util/http/http.service";
import {LoginForm} from "../model/loginForm";
import {UserEntity} from "../model/userEntity";
import {PromptService} from "../../util/prompt/prompt.service";
import {RoleEditForm} from "../model/roleEditForm";
import {UserEditForm} from "../model/userEditForm";
import {CurrentUserInfo} from "../model/currentUserInfo";
@Injectable()
export class UserService {

    constructor(private httpService: HttpService, private promptService: PromptService) {

    }

    deleteUser(id: number): Promise<any> {
        let promise: any = this.httpService.post("/api/user/delete_user", {id: id})
        return promise.then(function (data: any) {
            return Promise.resolve(data);
        })
    }

    editUser(info: UserEditForm): Promise<any> {
        let promise: any = this.httpService.post("/api/user/edit_user", info)
        return promise.then(function (data: any) {
            return Promise.resolve(data);
        })
    }

    changeUserPassword(info: UserEditForm): Promise<any> {
        let promise: any = this.httpService.post("/api/user/change_user_password", info)
        return promise.then(function (data: any) {
            return Promise.resolve(data);
        })
    }


    login(info: LoginForm) {
        let promise: any = this.httpService.post("/api/site/login", info)
        return promise.then(function (data: any) {
            return Promise.resolve(data);
        })
    }

    logout() {
        let promise: any = this.httpService.post("/api/site/logout", "")
        return promise.then(function (data: any) {
            return Promise.resolve(data);
        })
    }


    currentUser: CurrentUserInfo


    clearCurrentUserInfo() {
        this.currentUser = undefined;
    }

    getCurrentUserInfo(): Promise<CurrentUserInfo> {
        if (!this.currentUser) {
            let promise: any = this.httpService.post("/api/user/current_user", "")
            return promise.then((data: any)=> {
                if (data.success) {
                    this.currentUser = Object.assign(new CurrentUserInfo(), data.result)
                    return Promise.resolve(this.currentUser);
                } else {
                    this.promptService.warning(data.error.message);
                }
            })
        } else {
            return Promise.resolve(this.currentUser);
        }
    }


    getRoleAllList(): Promise<any> {
        let promise: any = this.httpService.post("/api/user/get_role_allList", "")
        return promise.then(function (data: any) {
            return Promise.resolve(data);
        })
    }

    getPermissionAllList(): Promise<any> {
        let promise: any = this.httpService.post("/api/user/get_permission_allList", "")
        return promise.then(function (data: any) {
            return Promise.resolve(data);
        })
    }


    getRolePermissionAllList(id: number): Promise<any> {
        let promise: any = this.httpService.post("/api/user/get_role_permissionAllList", {id: id})
        return promise.then(function (data: any) {
            return Promise.resolve(data);
        })
    }

    editRole(info: RoleEditForm): Promise<any> {
        let promise: any = this.httpService.post("/api/user/edit_role", info)
        return promise.then(function (data: any) {
            return Promise.resolve(data);
        })
    }


    //用户删除前，获取相关信息，用于是否确认删除用户
    getUserDeleteInfo(id: number): Promise<any> {
        let promise: any = this.httpService.post("/api/user/get_user_deleteInfo", {id: id})
        return promise.then(function (data: any) {
            return Promise.resolve(data);
        })
    }
}